from selenium import webdriver
from datetime import datetime
from time import sleep

class JingdongSnappedUp():
    def setup_method(self):
        self.driver = webdriver.Chrome()

    def teardown_method(self):
        self.driver.quit()

    def jingdongSnappedUp(self):
        self.driver.get('https://www.jd.com/')
        self.driver.set_window_size(1550,878)
        while True:
            try:
                self.driver.find_element_by_link_text('jd_188997eru')
                print('登录成功')
                self.driver.get('https://item.jd.com/100006008775.html')
                startingTime = '2020-05-06 18:00:00'
                while True:
                    if datetime.now().strftime("%Y-%m-%d %H:%M:%S") == startingTime:
                        print('开始抢购')
                        self.driver.find_element_by_id('InitCartUrl').click()
                        sleep(3)
                        self.driver.find_element_by_id('GotoShoppingCart').click()
                        sleep(3)
                        self.driver.find_element_by_class_name('submit-btn').click()
                        sleep(3)
                        self.driver.find_element_by_class_name('checkout-submit').click()
                        print('购买成功')
                        break
                    print('等待开始')
            except:
                print('等待登录')

if __name__ == '__main__':
    jd = JingdongSnappedUp()
    jd.setup_method()
    jd.jingdongSnappedUp()
    jd.teardown_method()